home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Security / FULL - Iolo Drive Scrubber 3.5 / DriveScrubber3.exe / {code_GetDSDir} / dshtml.dll / 1033 / HTML / DATATABLE.JS < prev    next >
Text File  |  2008-02-07  |  11KB  |  463 lines

  1.  
  2. function DataTable()
  3. {
  4.   this.ID = "";
  5.   this.Class = "";
  6.   this.Style = "";
  7.   this.Width = "";
  8.   this.LabelClass = "NOT_ASSIGNED";
  9.   this.ValueClass = "NOT_ASSIGNED";
  10.   
  11.   this.ItemClass = "NOT_ASSIGNED";
  12.   this.AlternateItemClass = "NOT_ASSIGNED";
  13.   
  14.   this.Border = "0";
  15.   this.Cellspacing = "1";
  16.   this.Cellpadding = "3";
  17.   
  18.   this.Items = new Array();
  19.   this.Render = __RenderDataTable;
  20.   this.Add = __AddItemToDataTable;
  21.   this.AddWithKey = __AddItemToDataTableWithKey;
  22.   this.AddWithKeyByIndex = __AddItemToDataTableWithKeyByIndex;
  23.   
  24.   this.ChangeCellbyKey = __ChangeDataTableCellbyKey; // change cell by using primary key (does't work in firefox)
  25.   this.ChangeCell = __ChangeDataTableCell;
  26.   this.ChangeClassInRowByIndex = __ChangeClassInRowByIndex;
  27.   
  28.   this.DeleteRow = __DeleteRow; // delete row by using primary key
  29.   this.DeleteRowByIndex = __DeleteRowByIndex;
  30.   this.Alternate = __Alternate;
  31.   
  32.   this.Clear = __DataTableClearAllItems;
  33.   
  34.   this.Count = __DataTableRowCount;
  35.   
  36.   this._Rendered = false;
  37.   
  38.   this._ClientID = __ClientID;
  39. }
  40.  
  41. function __ClientID()
  42. {
  43.   return "datatable_tbl_" + this.ID;
  44. }
  45.  
  46. function __DataTableRowCount()
  47. {
  48.    var tbl = Get("datatable_tbl_" + this.ID);
  49.    
  50.    if ( tbl == null )
  51.       return 0;
  52.    
  53.    var tbody = tbl.childNodes[0];
  54.    return tbody.rows.length;
  55. }
  56.  
  57. function __DataTableClearAllItems()
  58. {
  59.    var tbl = Get("datatable_tbl_" + this.ID);
  60.    if ( tbl == null )
  61.       return;
  62.    var count = tbl.childNodes[0].rows.length;
  63.    
  64.    for( var x=0; x< count; x++ )
  65.      tbl.deleteRow(0);
  66. }
  67.  
  68. function DataTableItem()
  69. {
  70.   this.Items; // array of values
  71.   
  72.   // id of the parent table
  73.   this.ParentID = "";  
  74.   
  75.   this.PrimaryKey = false;
  76.   
  77.   this.Label;
  78.   this.Value;
  79.   
  80.   this.ItemClass = "NOT_ASSIGNED";
  81.   this.AlternateItemClass = "NOT_ASSIGNED";
  82.   
  83.   this.Render = __RenderDataTableItem;
  84. }
  85.  
  86.  
  87. function __RenderDataTable()
  88. {
  89.   var content = "";
  90.   content += "<table id=\"datatable_tbl_" + this.ID + "\"cellspacing=\"" + this.Cellspacing + "\" cellpadding=\"" + this.Cellpadding + "\" border=\"" + this.Border + "\"";
  91.   
  92.   if (this.Width != "")
  93.     content += " width=\"" + this.Width + "\"";
  94.     
  95.     
  96.   if (this.Class != "")
  97.     content += " class=\"" + this.Class + "\"";
  98.   
  99.   
  100.   content += ">";
  101.   
  102.   for (var x = 0; x < this.Items.length; x++)
  103.   {
  104.     content += this.Items[x].Render(x);
  105.   }
  106.   
  107.   content += "</table>";
  108.   
  109.   this._Rendered = true;  
  110.   
  111.   
  112.   return content;
  113. }
  114.  
  115. function __RenderDataTableItem()
  116. {
  117.   var content = "";
  118.   
  119.   var id = "";
  120.   var start = 0;
  121.   
  122.   if ( this.Items[0] != "" && this.Items[0] != null && this.PrimaryKey)
  123.   {
  124.     id = "id=\"tr_" + this.ParentID + "_" + this.Items[0] + "\"";  
  125.     start = 1;
  126.   }
  127.   
  128.   if ( arguments.length )
  129.   {
  130.             var x = arguments[0];
  131.             if(x % 2 == 0)
  132.      {
  133.        content += "<tr valign=\"top\"  "+ id  +" class=\""+ this.ItemClass +"\">";
  134.      }
  135.      else
  136.      {
  137.        content += "<tr valign=\"top\"  "+ id  +" class=\""+ this.AlternateItemClass +"\">";
  138.      }   
  139.     }
  140.     else
  141.     {
  142.    content += "<tr valign=\"top\"  "+ id  +" >";
  143.   }
  144.   
  145.   
  146.   
  147.   for ( x = start; x < this.Items.length; x++)
  148.   {
  149.     content += "<td >";
  150.     content += this.Items[x];
  151.     content += "</td>";
  152.   }
  153.   content += "</tr>";
  154.   
  155.   return content;
  156. }
  157.  
  158. // Adding a new row to the table 
  159. function __AddItemToDataTable()
  160. {
  161.   var item = new DataTableItem();
  162.   item.ParentID = this.ID;
  163.   
  164.    // values 
  165.   item.Items = arguments;
  166.   
  167.   // array of values passed   
  168.    if(   arguments[0].length > 1 && arguments.length==1)
  169.      item.Items[0] = arguments[0];
  170.   
  171.   //item.Label = label;
  172.   //item.Value = value;
  173.   item.LabelClass = this.LabelClass;
  174.   item.ValueClass = this.ValueClass;
  175.   
  176.   item.ItemClass = this.ItemClass;
  177.   item.AlternateItemClass = this.AlternateItemClass;
  178.  
  179.  if( Get("datatable_tbl_" + this.ID) != null )
  180.   {
  181.     // The data table has already been rendered, so we need to manually 
  182.     // add the data table item.
  183.     var tbl = Get("datatable_tbl_" + this.ID);
  184.     
  185.     if ( tbl == null )
  186.       return;
  187.      
  188.       var tbody ;
  189.     var row ;
  190.     
  191.     
  192.     //workaround for firefox
  193.     if ( tbl.childNodes.length == 0 )
  194.      {
  195.        //firefox
  196.        row=tbl.insertRow(0);
  197.      }
  198.       else
  199.      {
  200.        // ie 
  201.       tbody = tbl.childNodes[0];
  202.       row = tbody.insertRow(tbody.rows.length);
  203.      }
  204.     
  205.     var cell = null;
  206.     
  207.      for (var x = 0; x < item.Items.length; x++)
  208.     {
  209.      cell = row.insertCell(row.cells.length);
  210.      cell.innerHTML = item.Items[x];
  211.     }
  212.   }
  213.  
  214.   this.Items[this.Items.length] = item;
  215.   
  216.   // recalculate row background colors
  217.   this.Alternate();
  218.   
  219. }
  220.  
  221. // Adding a new row to the table 
  222. // Example : suppose columns = 3, "__AddItemToDataTable(pk, v1, v2, v3)" 
  223. // the first value is taken as the primarykey and will not add to the row
  224. function __AddItemToDataTableWithKey()
  225. {
  226.   var item = new DataTableItem();
  227.   item.ParentID = this.ID;
  228.   
  229.   item.PrimaryKey = true;
  230.   
  231.    // values array
  232.   item.Items = arguments;
  233.   
  234.   // array of values passed   
  235.    if(   arguments[0].length > 1 && arguments.length==1)
  236.      item.Items[0] = arguments[0];
  237.   
  238.   //item.Label = label;
  239.   //item.Value = value;
  240.   item.LabelClass = this.LabelClass;
  241.   item.ValueClass = this.ValueClass;
  242.   
  243.   item.ItemClass = this.ItemClass;
  244.   item.AlternateItemClass = this.AlternateItemClass;
  245.   
  246.    var tbl = Get("datatable_tbl_" + this.ID);
  247.  
  248.   if (tbl != null)
  249.   {
  250.     // The data table has already been rendered, so we need to manually 
  251.     // add the data table item.
  252.     
  253.     var tbody ;
  254.     var row ;
  255.     
  256.     //workaround for firefox
  257.     if ( tbl.childNodes.length == 0 )
  258.      {
  259.        //firefox
  260.        row=tbl.insertRow(0);
  261.      }
  262.       else
  263.      {
  264.        // ie 
  265.       tbody = tbl.childNodes[0];
  266.       row = tbody.insertRow(tbody.rows.length);
  267.      }
  268.    
  269.     //take the first value as the primary key
  270.     if (  item.Items[0] != null)
  271.     {
  272.       // adding the primary key to the table row 'id' 
  273.       var rowid = "tr_" + this.ID + "_" + item.Items[0];  
  274.        row.setAttribute('id', rowid );
  275.     }
  276.     
  277.     var cell = null;
  278.     
  279.      for (var x = 1; x < item.Items.length; x++)
  280.     {
  281.      cell = row.insertCell(row.cells.length);
  282.      cell.innerHTML = item.Items[x];
  283.     }
  284.     
  285.   }
  286.  
  287.   this.Items[this.Items.length] = item;
  288.   
  289.   // recalculate row background colors
  290.   this.Alternate();
  291.   
  292. }
  293.  
  294.  
  295.  
  296. // Adding a new row to the table 
  297. // Example : suppose columns = 3, "__AddItemToDataTable(pk,index, v1, v2, v3)" 
  298. // the first value is taken as the primarykey and will not add to the row
  299. function __AddItemToDataTableWithKeyByIndex()
  300. {
  301.   var item = new DataTableItem();
  302.   item.ParentID = this.ID;
  303.   
  304.   item.PrimaryKey = true;
  305.   
  306.    // values array
  307.   item.Items = arguments;
  308.   
  309.   // array of values passed   
  310.    if(   arguments[0].length > 1 && arguments.length==1)
  311.      item.Items[0] = arguments[0];
  312.   
  313.   //item.Label = label;
  314.   //item.Value = value;
  315.   item.LabelClass = this.LabelClass;
  316.   item.ValueClass = this.ValueClass;
  317.   
  318.   item.ItemClass = this.ItemClass;
  319.   item.AlternateItemClass = this.AlternateItemClass;
  320.   
  321.    var tbl = Get("datatable_tbl_" + this.ID);
  322.  
  323.   if (tbl != null)
  324.   {
  325.     // The data table has already been rendered, so we need to manually 
  326.     // add the data table item.
  327.     
  328.     var tbody ;
  329.     var row ;
  330.     
  331.       var index = item.Items[1] 
  332.     //workaround for firefox
  333.     if ( tbl.childNodes.length == 0 )
  334.      {
  335.        //firefox
  336.        row=tbl.insertRow(index);
  337.      }
  338.       else
  339.      {
  340.        // ie 
  341.       tbody = tbl.childNodes[0];
  342.       row = tbody.insertRow(index);
  343.      }
  344.    
  345.     //take the first value as the primary key
  346.     if (  item.Items[0] != null)
  347.     {
  348.       // adding the primary key to the table row 'id' 
  349.       var rowid = "tr_" + this.ID + "_" + item.Items[0];  
  350.        row.setAttribute('id', rowid );
  351.     }
  352.     
  353.   
  354.     
  355.     var cell = null;
  356.     
  357.      for (var x = 2; x < item.Items.length; x++)
  358.     {
  359.      cell = row.insertCell(row.cells.length);
  360.      cell.innerHTML = item.Items[x];
  361.     }
  362.     
  363.   }
  364.  
  365.   this.Items[this.Items.length] = item;
  366.   
  367.   // recalculate row background colors
  368.   this.Alternate();
  369.   
  370. }
  371.  
  372. function __ChangeDataTableCellbyKey( text , pkValue , column )
  373. {
  374.    var tbl = Get("datatable_tbl_" + this.ID);
  375.    var rowID = "tr_" + this.ID + "_" + pkValue;
  376.    var row = Get ( rowID );
  377.    if( row == null )
  378.      return;
  379.    this.ChangeCell( text ,  row.rowIndex, column );
  380. }
  381.  
  382. function __ChangeDataTableCell(text, row, column)
  383. {
  384.   var element = document.getElementById("datatable_tbl_" + this.ID);
  385.   var tbody = element.childNodes[0];
  386.   var row = tbody.rows[row];
  387.   var cell = row.cells[column];
  388.   cell.innerHTML = text;
  389. }
  390.  
  391. function __ChangeClassInRowByIndex(index, Class)
  392. {
  393.   var element = document.getElementById("datatable_tbl_" + this.ID);
  394.   if(element == null)
  395.   return;
  396.   var tbody = element.childNodes[0];
  397.   var row = tbody.rows[index];
  398.   row.className = Class;
  399. }
  400.  
  401. //delete row by primary key
  402. function __DeleteRow( pkValue )
  403. {
  404.    var tbl = Get("datatable_tbl_" + this.ID);
  405.    var rowID = "tr_" + this.ID + "_" + pkValue;
  406.    var row = Get ( rowID );
  407.    
  408.    if( row == null )
  409.      return;
  410.    
  411.    this.DeleteRowByIndex( row.rowIndex );
  412. }
  413.  
  414. function __DeleteRowByIndex( index )
  415. {
  416.   var tbl = Get("datatable_tbl_" + this.ID);
  417.     
  418.     // table is not found or index is out of range
  419.     if ( tbl == null || ( tbl.rows.length < index+1)) 
  420.       return;
  421.     else
  422.      this._Rendered = true;
  423.      
  424.   tbl.deleteRow( index );
  425.   
  426.   //recalculate row background colours
  427.   this.Alternate();
  428. }
  429.  
  430. function __Alternate()
  431. {
  432.  
  433.  // if table is not rendered return 
  434.   var tbl = Get("datatable_tbl_" + this.ID);
  435.  if (! tbl ==null )
  436.     return;
  437.  
  438.   if (this.ItemClass == "NOT_ASSIGNED" )
  439.    return;
  440.   
  441.   if (this.AlternateItemClass == "NOT_ASSIGNED" )
  442.    this.AlternateItemClass = this.ItemClass;
  443.    
  444.    var table = Get("datatable_tbl_" + this.ID);  
  445.    
  446.    if (table == null )
  447.       return;
  448.    
  449.   var rows = table.rows; 
  450.   
  451.    for(i = 0; i < rows.length; i++)
  452.    {          
  453.      if(i % 2 == 0)
  454.      {
  455.        rows[i].className = this.ItemClass;
  456.      }
  457.      else
  458.      {
  459.        rows[i].className = this.AlternateItemClass;
  460.      }      
  461.    } 
  462.    
  463. }